package com.xzlcPT.controller;

import com.util.PageBean;
import com.xzlcPT.bean.XzField;
import com.xzlcPT.service.XzFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/10.
 */
@Controller
@RequestMapping("Field")
public class FieldController {
    @Autowired
    private XzFieldService fieldService;

    @ResponseBody
    @RequestMapping("selByType")
    public Map selByType(int type){
        Map<String,Object> map = new HashMap<>();
        List<XzField> fields = fieldService.selectByType(type);
        map.put("fieldList",fields);
        return map;
    }

    @RequestMapping("editFields.do")
    public ModelAndView editFields(XzField xzField){
        ModelAndView mv = new ModelAndView("index");
        List<XzField> list = new ArrayList();
        xzField.setFieldType("1");
        list.add(xzField);
        xzField.setFieldType("2");
        list.add(xzField);
        xzField.setFieldType("3");
        list.add(xzField);
        fieldService.insertByName(list);
        return mv;
    }

    @RequestMapping("deleteByName.do")
    public ModelAndView deleteByNames(XzField xzField){
        ModelAndView mv = new ModelAndView("index");
        fieldService.deleteByName(xzField.getFieldName());
        return mv;
    }

    //后台管理员方法---------------------
    //后台首页查询所有
    @RequestMapping("selAllByEmp.emp")
    public ModelAndView selAllByEmp(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int row,XzField xzField){
        ModelAndView mv = new ModelAndView("");
        PageBean<XzField> pageBean = fieldService.selAllByEmp(page,row,xzField);
        mv.addObject("xzField",xzField);
        mv.addObject("list",pageBean.getList());
        mv.addObject("page",pageBean.getPageNum());
        mv.addObject("pages",pageBean.getPages());
        mv.addObject("total",pageBean.getTotal());
        return mv;
    }
    //后台根据Id查询
    @RequestMapping("selFieldById.emp")
    public ModelAndView selFieldById(Long fieldId){
        ModelAndView mv = new ModelAndView("");
        XzField xzField = fieldService.selFieldById(fieldId);
        mv.addObject("xzField",xzField);
        return mv;
    }
    //后台管理员修改方法
    @RequestMapping("updateField.emp")
    public ModelAndView updateField(XzField xzField){
        ModelAndView mv = new ModelAndView("");
        Map map = new HashMap();
        int i = fieldService.updateField(xzField);
        if(i==1){
            map.put("msg","ok");
        }else {
            map.put("msg","err");
        }
        mv.addObject("map",map);
        return mv;
    }
//移动端----------------------------------------------------------
    @ResponseBody
    @RequestMapping("selByType.go")
    public Map selByTypeMob(int type){
        Map<String,Object> map = new HashMap<>();
        List<XzField> fields = fieldService.selectByType(type);
        map.put("fieldList",fields);
        return map;
    }


}
